window.onload = function() {

    let username = document.getElementById("username")
    let password = document.getElementById("password")
    console.log(username.value)

    // username.addEventListener("input", (e) => {
    //     console.log("正在输入", e.target.value)
    // })

    let btn = document.querySelector("button")
    btn.addEventListener("click", (e) => {
        e.preventDefault()

        let name = username.value
        let passd = password.value

        // let xhr = new XMLHttpRequest()
        // xhr.open("POST", "http://172.19.11.61:8080/login", true)
        // xhr.onreadystatechange = function() {
        //     if (xhr.readyState === 4 && xhr.status === 200) {
        //         console.log("请求成功")
        //     }
        // }

        // xhr.send()
        // console.log("这是发送后请求后的代码")

        //Promise(承诺)：是一种异步编程的解决方案，用于处理需要等待结果的操作
        let promise = new Promise((resolve, reject) => {
            // let success =true
            // if(success){
            //     resolve()//成功
            // }
            // else{
            //     reject()//失败拒绝
            // }
            // setTimeout(() => {
            //     console.log("五秒到了")
            //     resolve("今天天气不热")
            // }, 5000)
            let data = {
                username: name,
                password: passd

            }
            let xhr = new XMLHttpRequest()
            xhr.open("GET", "http://172.19.14.60:8080/login", true)
            xhr.setRequestHeader("content-Type", "application/json")

            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    console.log("请求成功")
                    resolve(xhr.responseText);
                }
            }
            console.log("开始发送请求")
            xhr.send(JSON.stringify(data))


        })
        promise.then((res) => {
            console.log("成功了，得到结果了")
            console.log(res)
        }).catch(() => {
            console.log("失败了")
        }).finally(() => {
            console.log("无论成功失败，都会到这里来")
        })





    })
}